package com.yc.project1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zwei
 * @create 2024-07-31 19:28
 */
@Repository
public class BankDaoImpl implements BankDao{        //dao层面向接口开发 先写一个dao层接口 然后写实现类
    @Autowired
    private DataSource dataSource;
    @Override
    public List<BankAccount> findAll() {
        List<BankAccount> list=new ArrayList<>();
        try(Connection con=dataSource.getConnection();
            PreparedStatement pstmt=con.prepareStatement("select * from bank"))
        {
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                BankAccount ba=new BankAccount();
                ba.setId(rs.getInt(1));
                ba.setBalance(rs.getDouble(2));
                list.add(ba);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
